package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.dto.BusinessEntityDTO;
import com.df.bpm.entity.BusinessEntity;
import com.df.bpm.service.impl.BusinessEntityDTOserviceImpl;
import com.df.bpm.service.impl.BusinessEntityServiceImpl;
import com.df.common.entity.Pagination;
import com.df.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;


/**
 * 业务主体控制器
 */
@Slf4j
@Tag(name = "业务主体控制器")
@RequestMapping("/businessEntity")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class BusinessEntityController {
    /**
     * 业务信息服务
     */
    public final BusinessEntityServiceImpl businessEntityServiceImpl;

    /**
     * 业务信息服务
     */
    public final BusinessEntityDTOserviceImpl businessEntityDTOserviceImpl;

    /**
     * 获取业务主体信息
     * @param businessEntityIndex 业务主体ID -1=未登记主体
     */
    @Operation(summary = "获取业务主体信息", description = "获取业务主体信息")
    @GetMapping("id")
    @SaIgnore
    public ResponseEntity<BusinessEntity> getOne(@RequestParam Integer businessEntityIndex) {
        return ResponseEntity.ok(businessEntityServiceImpl.getOne(new BusinessEntity().setBusinessEntityIndex(businessEntityIndex)));
    }

    /**
     * 更新（主体信息）
     * @param businessEntity 主体信息
     */
    @Operation(summary = "更新（主体信息）", description = "更新（主体信息）")
    @PutMapping("")
    @SaIgnore
    public ResponseEntity<?> update(@RequestBody BusinessEntity businessEntity){
        if(businessEntity.getBusinessEntityIndex()==null){
            return ResponseEntity.ok(businessEntityServiceImpl.add(businessEntity));
        }
        return ResponseEntity.ok(businessEntityServiceImpl.update(businessEntity));
    }

    /**
     * 新增（主体信息）
     * @param businessEntity 主体信息
     */
    @Operation(summary = "新增（主体信息）", description = "新增（主体信息）")
    @PostMapping("")
    @SaIgnore
    public ResponseEntity<?> newBusinessEntity(@RequestBody BusinessEntity businessEntity){
        return ResponseEntity.ok(businessEntityServiceImpl.add(businessEntity));
    }

    /**
     * 获取业务主体信息
     * @param businessEntityIndex 业务主体ID -1=未登记主体
     */
    @Operation(summary = "获取业务主体信息", description = "获取业务主体信息")
    @DeleteMapping("")
    @SaIgnore
    public ResponseEntity<Integer> delete(Integer businessEntityIndex) {
        return ResponseEntity.ok(businessEntityServiceImpl.delete(Collections.singleton(businessEntityIndex)));
    }

    /**
     * 业务主体DTO信息模糊查询
     * @param temp 业务主体DTO线索
     */
    @Operation(summary = "获取业务主体信息", description = "获取业务主体信息")
    @GetMapping("dto4Like")
    @SaIgnore
    public ResponseEntity<?> getDTOlist4Like(Pagination pagination,BusinessEntityDTO temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(businessEntityDTOserviceImpl.getList4Like(temp)));
        } else {

            return ResponseEntity.ok(businessEntityDTOserviceImpl.getList4Like(temp));
        }
    }

}
